<template>
    <section>
        <!--搜索工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px; ">
            <el-form :inline="true" :model="filters" ref="filters" label-width="80px">

                <el-form-item prop="full_name">
                    <el-input
                            placeholder="客户姓名/企业名称"
                            v-model.trim="filters.full_name">
                    </el-input>
                </el-form-item>
                <el-form-item prop="identity_no">
                    <el-input
                            placeholder="客户证件号码/企业执照号码"
                            v-model.trim="filters.identity_no">
                    </el-input>
                </el-form-item>
                <el-form-item prop="sale_name">
                    <el-input
                            placeholder="销售姓名"
                            v-model.trim="filters.sale_name">
                    </el-input>
                </el-form-item>
                <el-form-item prop="type">
                    <el-select
                            placeholder="客户类型" clearable v-model="filters.type" size="18px">
                        <el-option key="" label="全部" value=""></el-option>
                        <el-option :key="value" :label="txt" :value="value"
                                   v-for="(txt, value) in options.customer_type"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getCustomers">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button @click="resetForm('filters')">清空</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button @click="exportExcel">导出</el-button>
                </el-form-item>

            </el-form>
        </el-col>
        <!--搜索结果表格-->
        <el-table
                :header-row-style="headerRowStyle"
                :data="tableData"
                stripe
                v-loading="listLoading"
                style="width: 100%;">
            <el-table-column
                    label="客户姓名/企业名称"
                    align="center">
                <template slot-scope="scope">
                    <el-button
                            @click="customerInfo(scope.row.id)"
                            type="text"
                            size="small">
                        {{ scope.row.full_name }}
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="客户证件号码/企业执照编码"
                    prop="identity_no">
            </el-table-column>
            <el-table-column
                    align="center"
                    label="投资金额"
                    :formatter="formatterAmount">
            </el-table-column>
            <el-table-column
                    align="center"
                    label="销售姓名"
                    prop="sale_name">
            </el-table-column>
            <el-table-column
                    align="center"
                    label="部门"
                    prop="sale_department_name">
            </el-table-column>
            <el-table-column
                    align="center"
                    label="组别"
                    prop="sale_team_name">
            </el-table-column>
        </el-table>
        <!--分页-->
        <el-col :span="24" class="toolbar">
            <el-pagination
                    style="text-align: center;"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :page-size="pager.page_size"
                    :page-sizes="pager.sizes"
                    background
                    :current-page="pager.page_no"
                    layout="prev,pager,next,sizes,total"
                    :total="pager.total">
            </el-pagination>
        </el-col>
    </section>
</template>

<script>
    import * as api from '../../api/customer_api'
    import dict from '../../common/js/dict';
    import {fileDownload} from '../../common/js/download';

    export default {
        data() {
            return {
                //表单header样式
                headerRowStyle: {
                    backgroundColor: "#ddd",
                    color: "#333",
                    textAlign: "center"
                },
                //搜索条件
                filters: {
                    full_name: "",
                    identity_no: "",
                    sale_name: "",
                    type: ""
                },
                //分页设置
                pager: {
                    sizes: [10, 20, 30, 40, 50],
                    total: 0,
                    page_size: 10,
                    page_no: 1
                },
                options: {
                    customer_type: dict.customer_type
                },
                //表格数据
                tableData: [],
                listLoading: false
            };
        },
        methods: {
            customerInfo(id){
                this.$router.push('info/' + id);
            },
            formatterAmount(row) {
                if(row.amount)
                {
                    return (row.amount / 100);
                }
                return '';
            },
            formatterType(row) {
                return this.options.customer_type[row.type];
            },
            //分页page_size发生改变
            handleSizeChange(val) {
                this.pager.page_no = 1;
                this.pager.page_size = val;
                this.getCustomers();
            },
            //分页page_no发生改变
            handleCurrentChange(val) {
                this.pager.page_no = val;
                this.getCustomers();
            },
            //根据条件搜索客户数据-实体方法
            getCustomers() {
                this.listLoading = true;  //显示loading

                let condition = {
                    full_name: this.filters.full_name,
                    identity_no: this.filters.identity_no,
                    sale_name: this.filters.sale_name,
                    type: this.filters.type
                };
                let params = {
                    page_size: this.pager.page_size,
                    page_no: this.pager.page_no,
                    condition: condition
                };
                api.customerList(params).then((res) => {
                    let result = JSON.parse(res.data.datas);
                    if (res.data.status == 200) {
                        this.listLoading = false;  //隐藏loading
                        this.tableData = result.items;
                        this.pager.total = result.total;
                    } else {
                        if (res.data.status == 401) {
                            this.$router.push('/login');
                        }
                        this.$message.error(res.data.status + " " + res.data.msg);
                    }
                });
            },
            //重置搜索表彰
            resetForm(formName) {
                this.$refs[formName].resetFields();
                this.pager.page_no = 1;
                this.getCustomers();
            },
            exportExcel(){

                let condition = {
                    full_name: this.filters.full_name,
                    identity_no: this.filters.identity_no,
                    sale_name: this.filters.sale_name,
                    type: this.filters.type
                };
                let params = {
                    page_size: this.pager.page_size,
                    page_no: this.pager.page_no,
                    condition: condition
                };
                api.customerExport(params).then((res) => {
                    if (res.status === 200) {
                        fileDownload(res, this);
                    } else {
                        if (res.data.status == 401) {
                            //this.$router.push('/login');
                        }
                        this.$message.error(res.data.msg);
                    }
                }).catch((e) => {
                    this.$message({
                        type: 'info',
                        message: e.message
                    });
                });
            },
            //初始化页面
            initData() {
                this.getCustomers();
            }
        },
        mounted() {
            this.$nextTick(function () {
                this.initData();  //初始化页面数据
            })

        }
    }
</script>

<style scoped>

</style>